package com.webinsky.persist.dao.catalog;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;

import com.webinsky.common.utils.db.Pagnation;
import com.webinsky.persist.entity.catalog.VCatalog;

public interface CataglogMapper {
    @Insert({
        "insert into catalog (name, sort, language, del, create_at, update_at)",
        "values (#{name,jdbcType=VARCHAR}, #{sort,jdbcType=INTEGER}, ",
        "#{language,jdbcType=VARCHAR}, #{del,jdbcType=INTEGER}, now(), now())"
    })
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Integer.class)
    int insert(VCatalog record);
    
    @Select({
        "select",
        "id, name, sort, language, del, create_at, update_at",
        "from catalog"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
        @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
        @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
        @Result(column="create_at", property="create_at", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_at", property="update_at", jdbcType=JdbcType.TIMESTAMP)
    })
    List<VCatalog> selectAll();
    
    @Select({
        "select",
        "id, name, sort, language, del, create_at, update_at",
        "from catalog",
        "where id = #{id, jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
        @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
        @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
        @Result(column="create_at", property="create_at", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_at", property="update_at", jdbcType=JdbcType.TIMESTAMP)
    })
    VCatalog selectById(@Param("id")int id);
    
    @SelectProvider(type = CatalogProvider.class, method = "selectByPagnation")
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
        @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
        @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
        @Result(column="create_at", property="create_at", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_at", property="update_at", jdbcType=JdbcType.TIMESTAMP)
    })
    List<VCatalog> selectByPagnation(Pagnation pagnation);
    
    @Select({
        "select",
        "count(*)",
        "from catalog"
    })
    int selectCount();
    
    @Update({
        "update catalog",
        "set name = #{name,jdbcType=VARCHAR},",
        "login_password = #{login_password,jdbcType=VARCHAR},",
        "sort = #{sort,jdbcType=VARCHAR},",
        "language = #{language, jdbcType=VARCHAR}",
        "del = #{del,jdbcType=INTEGER},",
        "create_at = #{create_at,jdbcType=TIMESTAMP},",
        "update_at = #{update_at,jdbcType=TIMESTAMP}",
        "where Id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(VCatalog record);
}
